#!/bin/env ruby
# encoding: utf-8
require_relative '../spec/spec_helper'
require_relative '../spec/commons/user_auth'
require_relative '../spec/commons/check_pages'
#require_relative '../spec/commons/report'

describe 'Employees page' do
  before {

  }

  context 'when logged in' do
  	it 'then he can work with employees list page' do
  	  goto 'http://pulse.7bits.it/en/login-page'
  	  sign_in('adm.company2@mail.ru','123qwe+')
  	  goto 'http://pulse.7bits.it/en/us/admin/employees-list'
  	  check_employees()
  	end
  end

  context "if file isn't selected" do
    it 'then  button "Upload" is disabled' do
      goto 'http://pulse.7bits.it/en/us/admin/employees-list'
      if file_field(id: "upload-employees-input", name: "uploadedFile").value() == nil then
        (button(class: "btn btn-info", type: "submit", text: "Upload CSV with employees list").enabled?).should be_falsey
      end
    end  
  end

  context "when submit a file" do
    it "then  max allowed size is 100 KB" do
      goto 'http://pulse.7bits.it/en/us/admin/employees-list'
      file_field(id: "upload-employees-input", name: "uploadedFile").set("/home/sevenbits/testing-watir/acceptance_tests/spec/files/big_file.csv")
      button(class: "btn btn-info", type: "submit", text: "Upload CSV with employees list").click
      span(text: "Max allowed size is 100 KB.").wait_until_present
    end
  end

  #“Invalid extension of file (must be CSV)”
  context "when submit a file" do
    it "then file must have CSV extention"  do
      goto 'http://pulse.7bits.it/en/us/admin/employees-list'      
      file_field(id: "upload-employees-input", name: "uploadedFile").set("/home/sevenbits/testing-watir/acceptance_tests/spec/files/bad_extention.css")
      button(class: "btn btn-info", type: "submit", text: "Upload CSV with employees list").click
      (span(id:  "error-block", class: "help-block dark-red").text.index("Invalid extension of file (must be CSV).") !=nil ).should be_truthy

      goto 'http://pulse.7bits.it/en/us/admin/employees-list'
      file_field(id: "upload-employees-input", name: "uploadedFile").set("/home/sevenbits/testing-watir/acceptance_tests/spec/files/bad_extention.csv.css")
      button(class: "btn btn-info", type: "submit", text: "Upload CSV with employees list").click
      (span(id:  "error-block", class: "help-block dark-red").text.index("Invalid extension of file (must be CSV).") !=nil ).should be_truthy
    end
  end
  
  context "when submit file" do
    it "then you can't upload empty file" do
      file_field(id: "upload-employees-input", name: "uploadedFile").set("/home/sevenbits/testing-watir/acceptance_tests/spec/files/empty_file.csv")
      button(class: "btn btn-info", type: "submit", text: "Upload CSV with employees list").click
      span(id: "error-block", class: "help-block dark-red", text: "Empty file.").wait_until_present
    end
  end
  
  context 'when logged out' do
    it "then he can't work with employees list page"  do
	  user_logout()
    end
  end
  	
end